function setUnivername() {
    univername = document.getElementById("university").options[document.getElementById("university").selectedIndex].value;
    var servreg = "/voteup/registergetfacs";
    doc = new XMLHttpRequest();
    doc.open("POST", servreg, true);
    doc.onreadystatechange = updatePage;
    doc.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    doc.send("univername=" + univername);
}

function updatePage() {
    if (doc.readyState == 4) {
        var response = doc.responseText;
        var options = response.split('\r\n');
        var select = document.getElementById("faculty");
        select.options.length = 0;
        if (response.length == 0) return;
        var index = 0;
        for (var i in options) {
            val = options[i];
            select.options[select.options.length] =
                    new Option(val, val, false, false)
            if (val == "${person.group.faculty.facultyName}") {
                select.selectedIndex = i;
            }
        }
        select.options.length--;

        setFacultyname();
    }
}

function setFacultyname() {

    facultyname = document.getElementById("faculty").options[document.getElementById("faculty").selectedIndex].value;
    univername = document.getElementById("university").options[document.getElementById("university").selectedIndex].value;
    var servreg = "/voteup/registergetgroups";
    doc = new XMLHttpRequest();
    doc.open("POST", servreg, true);
    doc.onreadystatechange = getGroups;
    doc.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    doc.setRequestHeader("Cache-Control", "No-Cache");
    doc.send("facultyname=" + facultyname + "&univername=" + univername);
}


function getGroups() {
    if (doc.readyState == 4) {
        var response = doc.responseText;
        var groups = response.split('\r\n');
        groups.length--;
        $("#group").autocomplete().flushCache();
        fill(groups);
    }
}

function fill(arr) {
    $("#group").autocomplete(arr, {
                delay:40,
                minChars:1,
                matchSubset:1,
                autoFill:true,
                cacheLength:1,
                maxItemsToShow:10
            });
}